<template>
  <div class="P_Detail">
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="2000" indicator-color="#8B0000">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" alt="" />
      </van-swipe-item>
    </van-swipe>
    <!-- Tips组件 -->
    <Tips />
    <!-- Title -->
    <div class="title">
      <h2 class="t">{{ info.name }}</h2>
      <h3 class="c">{{ info.goods_brief }}</h3>
      <span class="b">{{ info.retail_price | RMBFormat }}</span>
    </div>
    <!-- 弹出层 -->
    <div class="Popup">
      <van-cell is-link @click="SkuShow = true">展示弹出层</van-cell>
    </div>
    <!-- 商品参数 -->
    <div class="s_params">
      <h2>商品参数</h2>
      <ul>
        <li v-for="(item02, index) in attribute" :key="index">
          <span>{{ item02.name }}:</span>
          <span>{{ item02.value }}</span>
        </li>
      </ul>
    </div>
    <!-- 商品内容 -->
    <div class="shop_Detail">
      <div v-html="goods_desc" class="shop_Detail_show"></div>
    </div>
    <!-- 常见问题 -->
    <div class="issue">
      <div class="issue_top">
        <h2>· 常见问题 ·</h2>
      </div>
      <div>
        <ul>
          <li v-for="item03 in issue" :key="item03.id">
            <h2><span>·</span> {{ item03.question }}</h2>
            <h3>
              {{ item03.answer }}
            </h3>
          </li>
        </ul>
      </div>
      <h2 class="b">· 相关产品 ·</h2>
    </div>
    <!-- 相关产品 -->
    <div>
      <production :goodslist="goodslist" />
    </div>
    <!-- 商品导航 -->
    <div>
      <GoodsAction @changeSkuShow="changeSkuShow" :goodsCount="goodsCount" />
    </div>
    <!-- sku -->
    <div>
      <van-sku
        v-model="SkuShow"
        :sku="sku"
        :goods="goods"
        :hide-stock="sku.hide_stock"
        ref="sku"
      />
    </div>
  </div>
</template>

<script>
import {
  GetProductionDetail,
  GetGoodsrelated,
  GetCartQua,
  GetCartAdd,
} from "@/request/api";
import Tips from "@/components/ProuductionDetail/Tips";
import production from "@/components/production";
import GoodsAction from "@/components/GoodsAction";
export default {
  components: {
    Tips,
    production,
    GoodsAction,
  },
  methods: {
    // 加入购物车
    changeSkuShow() {
      let mustbeLogin = this.utils.mustbeLogin();
      if (!mustbeLogin) return;
      // console.log(123123);
      if (this.SkuShow) {
        let count = this.$refs.sku.getSkuData().selectedNum;
        // console.log(this.$refs.getSkuData());
        GetCartAdd({
          goodsId: this.$route.query.id,
          productId: this.productList[0].id,
          number: count,
        }).then((res) => {
          console.log(count);
          this.goodsCount = res.data.cartTotal.goodsCount;
          this.$toast.success("添加成功");
        });
      }
      this.SkuShow = !this.SkuShow;
    },
  },
  data() {
    return {
      // SKU是否显示
      SkuShow: false,
      // SKU
      sku: {
        tree: [],
        hide_stock: false, //是否隐藏剩余库存
        price: "", // 默认价格（单位元）
        stock_num: "", // 商品总库存
      },
      goods: {
        picture: "",
      },
      show: false,
      // 轮播图数组
      gallery: [],
      // Title数组
      info: [],
      //商品参数数组
      attribute: [],
      //商品内容数组
      goods_desc: "",
      // 常见问题数组
      issue: [],
      // 相关产品
      goodslist: [],
      // 获取用户购物车数量,是一个数字
      goodsCount: "",
      // 获取商品ID
      productList: [],
    };
  },
  created() {
    GetProductionDetail({ id: this.$route.query.id }).then((res) => {
      //打印ID
      // console.log(this.$route.query.id);
      //打印一下返回数据
      // console.log(res.data);
      // 解构res.data数据
      let { gallery, info, attribute, issue } = res.data;
      //更新轮播图数组
      this.gallery = gallery;
      // 更新Title数组
      this.info = info;
      // 更新商品参数数组
      this.attribute = attribute;
      // 更新商品内容
      this.goods_desc = res.data.info.goods_desc;
      // console.log(this.goods_desc);
      // 更新常见问题数组
      this.issue = issue;
      // SKU数据渲染
      this.goods.picture = res.data.info.list_pic_url;
      this.sku.price = res.data.info.retail_price;
      this.sku.stock_num = res.data.info.goods_number;
      // console.log(res.data.info);
      //更新productList数组
      // console.log(res.data);
      this.productList = res.data.productList;
    });
    GetGoodsrelated({ id: this.$route.query.id }).then((res) => {
      // console.log(res);
      let { goodsList } = res.data;
      // 更新相关产品数组
      this.goodslist = goodsList;
    });
    // 获取用户购物车数量
    GetCartQua().then((res) => {
      this.goodsCount = res.data.cartTotal.goodsCount;
      // console.log(res);
    });
  },
};
</script>
 
<style lang = "less" scoped>
.P_Detail {
  padding-bottom: 0.666667rem;
  /* 轮播图 */
  .van-swipe {
    img {
      height: 3.413333rem;
      width: 100%;
    }
  }
  /* title */
  .title {
    height: 1.866667rem;
    background-color: #fff;
    margin-top: 0.133333rem;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .t,
    .c,
    .b {
      font-size: 0.213333rem;
      /* font-weight: bold; */
    }
    .c {
      font-size: 0.146667rem;
    }
    .b {
      font-size: 0.133333rem;
      color: #8b0000;
    }
  }
  /* 商品参数 */
  .s_params {
    margin-top: 0.266667rem;
    background-color: #fff;
    height: 3.333333rem;
    h2 {
      font-size: 0.213333rem;
      padding: 0.133333rem;
    }
    ul {
      padding: 0.066667rem;
      li {
        background-color: #efefef;
        font-size: 0.133333rem;
        height: 0.32rem;
        border-radius: 0.066667rem;
        line-height: 0.32rem;
        width: 3.6rem;
        margin-top: 0.013333rem;
        span {
          display: block;
          float: left;
          &:nth-child(1) {
            width: 1.333333rem;
            text-align: right;
            color: #999999;
            font-size: 0.133333rem;
          }
        }
      }
    }
  }
  /* 商品内容 */
  /deep/.shop_Detail_show {
    img {
      display: block;
      max-width: 100%;
      font-size: 0.186667rem !important;
    }
  }
  /* 常见问题 */
  .issue {
    /* height: 4rem; */
    margin-top: 0.266667rem;
    .issue_top {
      font-size: 0.16rem;
      text-align: center;
    }
    ul {
      text-align: left;
      li {
        padding: 0.066667rem;
        h2 {
          span {
            font-size: 0.266667rem;
            color: #8b0000;
          }
          font-size: 0.16rem;
        }
        h3 {
          color: #666666;
          margin-top: 0.133333rem;
          margin-left: 0.2rem;
          font-size: 0.133333rem;
        }
      }
    }
    .b {
      margin-top: 0.133333rem;
      font-size: 0.16rem;
      text-align: center;
    }
  }
  /* 弹出层 */
  .Popup {
    margin-top: 0.053333rem;
  }
}
</style>